package com.caocaog.market.baidu;
//import com.baidu.ai.aip.utils.HttpUtil;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.gson.JsonObject;
import org.apache.commons.lang3.StringUtils;

/**
 * @author liguang
 * @version 1.0.0
 * @Project jeecg-boot
 * @Package com.caocaog.market.baidu
 * @ClassName ContentCheck.java
 * @createTime 2022年03月17日 15:13:00
 * @Description 百度涉黄信息检测——文本审核接口
 */
public class BaiduTextContentCheck {
    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    public static String textCensor(String content) {
        JsonObject jsonObject = new JsonObject();
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined";
        try {
            String param = "text="+content;
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
//            String accessToken = "[调用鉴权接口获取的token]";
            String accessToken = AuthService.getAuth();
            System.out.println("重新请求了accessToken----->>>>>>>");
            String result ="";
            try{
                System.out.println("accessToken---->   "+accessToken);
                // 24.e2ac762e5a8e5ee8a4f8d0df2bc9a968.2592000.1650102738.282335-25783894
                result = BaiduHttpUtil.post(url, String.valueOf(accessToken), param);
            }catch(Exception e){
                System.out.println("出错了，返回检测失败——————————————————————————————");
                jsonObject.addProperty("status",0);
                jsonObject.addProperty("msg","检测失败！");
                return jsonObject.toString();
            }
            if(StringUtils.isBlank(result)){
                jsonObject.addProperty("status",0);
                jsonObject.addProperty("msg","未能有效检测，检测失败！");
                return jsonObject.toString();
            }
            JSONObject obj = JSONUtil.parseObj(result);
            boolean flag = obj.containsKey("error_code");
            System.out.println("检测结果是否包含错误信息："+flag);
            if(flag){
                jsonObject.addProperty("status",0);
                jsonObject.addProperty("msg","检测出错，检测失败！");
                return jsonObject.toString();
            }else {
                Integer conclusionType = (Integer) obj.get("conclusionType");

                System.out.println(conclusionType);
                if (conclusionType != 1) {
                    jsonObject.addProperty("status",conclusionType);
                    String conclusion = (String)obj.get("conclusion");
                    JSONArray data = (JSONArray)obj.get("data");
                    JSONObject js = JSONUtil.parseObj(data.get(0));
                    System.out.println("返回的信息："+js);
                    System.out.println("returnData---->   "+js.get("msg"));
                    jsonObject.addProperty("msg",js.getStr("msg"));
                    return jsonObject.toString();
                }
                System.out.println("直接返回，合规");
                jsonObject.addProperty("status",1);
                jsonObject.addProperty("msg","内容合规，无异常！");
                return jsonObject.toString();
            }
            // {"conclusion":"不合规","log_id":16475072858604960,"data":[{"msg":"存在政治敏感不合规","conclusion":"不合规","hits":[{"wordHitPositions":[{"positions":[[0,2]],"label":"300102","keyword":"刁近平"}],"probability":1.0,"datasetName":"百度默认文本反作弊库","words":["刁近平"],"modelHitPositions":[[0,2,0.9922]]}],"subType":3,"conclusionType":2,"type":12}],"isHitMd5":false,"conclusionType":2}
            // {"conclusion":"疑似","log_id":16475073995585178,"data":[{"msg":"疑似存在文本色情不合规","conclusion":"疑似","hits":[{"wordHitPositions":[],"probability":0.82729053,"datasetName":"百度默认文本反作弊库","words":[],"modelHitPositions":[[0,1,0.8273]]}],"subType":2,"conclusionType":3,"type":12}],"isHitMd5":false,"conclusionType":3}
            // {"conclusion":"不合规","log_id":16475074415226794,"data":[{"msg":"存在文本色情不合规","conclusion":"不合规","hits":[{"wordHitPositions":[],"probability":0.992278,"datasetName":"百度默认文本反作弊库","words":[],"modelHitPositions":[[0,1,0.9923]]}],"subType":2,"conclusionType":2,"type":12}],"isHitMd5":false,"conclusionType":2}
            // {"conclusion":"合规","log_id":16475076055996059,"isHitMd5":false,"conclusionType":1}
            // 返回结果：conclusionType（Integer）审核结果类型，可取值1.合规，2.不合规，3.疑似，4.审核失败；conclusion（String）的值是对应的汉字：合规，不合规，疑似，审核失败
        } catch (Exception e) {
            System.out.println("检测出错，错误信息："+e.getMessage());
            jsonObject.addProperty("status",0);
            jsonObject.addProperty("msg","检测出错，未能有效检测！");
            return jsonObject.toString();
        }
    }

    public static void main(String[] args) {
        String res = BaiduTextContentCheck.textCensor("共产党");
        JSONObject obj = JSONUtil.parseObj(res);
        System.out.println(obj.get("status")+"      msg:"+obj.get("msg"));
    }
}